package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.User;
import com.zy.imagineCommunity.entity.UserText;
import com.zy.imagineCommunity.entity.UserTextTwo;
import com.zy.imagineCommunity.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserManagementController {

    @Autowired
    UserSerivce userSerivce;

    @GetMapping("/userManagement")
    public String getUserManagement() {
        return "/UserManagement";
    }

    @ResponseBody
    @PostMapping("/toUserManagement")
    public JSONObject userManagement(int page, String username, String userType) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<UserText> pageText = userSerivce.queryUserTextByPageNoAndPageSizeAndUserName(page, 10, username, userType);

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 修改用户状态
     *
     * @param id
     * @param userStatus
     */
    @ResponseBody
    @PostMapping("/updateAdminUser")
    public void updateAdminUser(int id, String userStatus) {
        //通过id查询用户
        User user = userSerivce.queryUserByUserId(id);

        int status = 0;
        //判断状态并设置
        if ("普通用户".equals(userStatus)) {
            status = 1;
        }
        if ("禁用".equals(userStatus)) {
            status = 2;
        }
        if ("超级管理员".equals(userStatus)) {
            status = 3;
        }
        if ("Java模块管理员".equals(userStatus)) {
            status = 4;
        }
        if ("C++模块管理员".equals(userStatus)) {
            status = 5;
        }
        if ("Python模块管理员".equals(userStatus)) {
            status = 6;
        }
        if ("JavaScript模块管理员".equals(userStatus)) {
            status = 7;
        }
        if ("c语言模块管理员".equals(userStatus)) {
            status = 8;
        }
        if ("云计算模块管理员".equals(userStatus)) {
            status = 9;
        }

        //通过id修改用户
        userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), status, user.getLanguageState(), user.getMessageNum()));

    }

    /**
     * 删除用户
     *
     * @param id
     */
    @ResponseBody
    @PostMapping("/deleteAdminUser")
    public void deleteAdminUser(int id) {
        userSerivce.deleteUserById(id);
    }

    /**
     * 权限管理-超级管理员
     *
     * @return
     */
    @GetMapping("/userRightsManagement")
    public String getRightsUserManagement(Model model) {
        model.addAttribute("type", 1);
        return "UserRightsManagement";
    }

    /**
     * 权限管理-板块管理员
     *
     * @return
     */
    @GetMapping("/userRightsManagementByType/{type}")
    public String getRightsUserManagementByType(@PathVariable("type") int type, Model model) {
        model.addAttribute("type", type);
        return "UserRightsManagementByType";
    }

    /**
     * 通过管理员类型返回用户(显示不通权限状态)
     *
     * @param type
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping("/toUserRightsManagement/{type}")
    public JSONObject userRightsManagement(@PathVariable("type") int type, int page) {
        JSONObject jsonObject = new JSONObject();

        //通过id查询用户
        Page<UserTextTwo> pageText = userSerivce.queryUserTextTwoByTypeAndPageNoAndPageSize(type, page, 10);

        for (UserTextTwo userText : pageText.getItems()) {
            System.out.println(userText);
        }

        jsonObject.put("code", 0);
        jsonObject.put("count", pageText.getPageCountTotal());
        jsonObject.put("data", pageText.getItems());

        return jsonObject;
    }

    /**
     * 禁言
     *
     * @param userId
     * @param adminUserStatus
     */
    @ResponseBody
    @PostMapping("/forbiddenSpeech")
    public void forbiddenSpeech(int userId, int adminUserStatus) {

        User user = userSerivce.queryUserByUserId(userId);

        if (adminUserStatus == 3) {
            String languageState = user.getLanguageState() + "1_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 4) {
            String languageState = user.getLanguageState() + "10_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 5) {
            String languageState = user.getLanguageState() + "11_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 6) {
            String languageState = user.getLanguageState() + "12_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 7) {
            String languageState = user.getLanguageState() + "13_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 8) {
            String languageState = user.getLanguageState() + "14_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }
        if (adminUserStatus == 9) {
            String languageState = user.getLanguageState() + "15_";
            userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
        }

    }

    /**
     * 解除禁言
     *
     * @param userId
     * @param adminUserStatus
     */
    @ResponseBody
    @PostMapping("/relieve")
    public void relieve(int userId, int adminUserStatus) {

        User user = userSerivce.queryUserByUserId(userId);

        if (adminUserStatus == 3) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"1".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 4) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"10".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 5) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"11".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 6) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"12".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 7) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"13".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 8) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"14".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }
        if (adminUserStatus == 9) {

            String stateString = user.getLanguageState();

            String[] stateNumber = stateString.split("_");

            if (stateNumber.length > 2) {
                String languageState = "";
                for (int i = 0; i < stateNumber.length; i++) {

                    if (!"15".equals(stateNumber[i])) {
                        languageState = languageState + stateNumber[i] + "_";
                    }

                }
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), languageState, user.getMessageNum()));
            } else {
                userSerivce.updateUserByUserId(new User(user.getId(), user.getUsername(), user.getPassword(), user.getEmail(), user.getHeadImg(), user.getCreateTime(), user.getUserStatus(), stateNumber[0] + "_", user.getMessageNum()));
            }

        }

    }

    /**
     * 批量删除用户
     *
     * @param idList
     */
    @ResponseBody
    @PostMapping("/deleteUserList")
    public void deleteUserList(String idList) {

        String[] id = idList.split(",");

        for (int i = 0; i < id.length; i++) {
            if (!"".equals(id[i])) {
                int idInt = Integer.parseInt(id[i]);
                userSerivce.deleteUserById(idInt);
            }
        }

    }

}
